//
//  HAODE
//
//  Created by panquanReabam.com on 15/9/23.
//  Copyright (c) 2015年 reabam.com. All rights reserved.
//

#import "OrderDeliveryDetailViewController.h"
#import "ZJSwitch.h"
#import "MJRefresh.h"
#import "payView.h"
#import "MemberListViewController.h"
#import "GoodsView.h"
#import "UIImageView+WebCache.h"
#import "MembershipDetailsViewController.h"
#import "DeliveryDetailView.h"
#import "DeliveryDetailModel.h"
@interface OrderDeliveryDetailViewController ()<MBProgressHUDDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UITextFieldDelegate,UITextViewDelegate,UISearchBarDelegate,UIScrollViewDelegate>

@end

@implementation OrderDeliveryDetailViewController

{
    
     DeliveryDetailView*CommonView ;
      UIScrollView*MYmainScroll;
      UIView*expressCompanyView;
    NSDictionary*dataDict;
    UIView*remarkLabelView;
    UILabel *remarkline;
}
@synthesize alt = alt;

-(void)BackAction:(UIButton *)sender{
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    
    
    
    self.navigationController.delegate = nil;
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
  
    [[self rdv_tabBarController] setTabBarHidden:YES];
    self.navigationController.navigationBarHidden=NO;
        self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;
    
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
    self.view.backgroundColor=[UIColor whiteColor];
}
-(void)viewDidLoad{
    [super viewDidLoad];
    self.title=@"配送详情";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor blackColor],
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];

    MYmainScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, ScreenWidth, ScreenHeight)];
    [MYmainScroll setBackgroundColor: zuiqianhuiColor];
    MYmainScroll.delegate = self;
    MYmainScroll.userInteractionEnabled = YES;
    MYmainScroll.scrollEnabled = YES;
    MYmainScroll.showsVerticalScrollIndicator = FALSE;
    MYmainScroll.showsHorizontalScrollIndicator = FALSE;
    [self.view addSubview:MYmainScroll];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue:self.orderId forKey:@"deliveryId"];
    
    
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    [HttpRequest requestData:@"Business/Order/DeliveryDetail" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    NSLog(@"====%@",severDataDict.dataDict);
                [Global hideProgressHUDInternal:self.view];
                dataDict=severDataDict.dataDict;
              
                CommonView = [[DeliveryDetailView alloc]init];
                DeliveryDetailModel*dataModel=[DeliveryDetailModel dataModelWithDeliveryDetailDict:severDataDict.dataDict];
                CommonView.DeliveryModel=dataModel;
                CommonView.frame=CGRectMake(0, 0, ScreenWidth, CommonView.heightView.bottom);
                [MYmainScroll addSubview:CommonView];
                
                if ([[NSString stringWithFormat:@"%@",  [[dataDict objectForKey:@"delivery"] objectForKey:@"deliveryStatus"]] isEqualToString:@"Y"]) {
                    expressCompanyView=[[UIView alloc]initWithFrame:CGRectMake(0, CommonView.bottom, ScreenWidth, 110)];
                    expressCompanyView.backgroundColor=[UIColor whiteColor];
                    [MYmainScroll addSubview:expressCompanyView];
                    UILabel*expressCompany=[[UILabel alloc]initWithFrame:CGRectMake(15, 19, 80, 15)];
                    expressCompany.text=@"物流信息";
                    expressCompany.textColor=qianheiColor;
                    expressCompany.font=[UIFont systemFontOfSize:15];
                    [expressCompanyView addSubview:expressCompany];
                    
                    UILabel*expressCompanyName=[[UILabel alloc]initWithFrame:CGRectMake(15, expressCompany.bottom+19, 130, 14)];
                    expressCompanyName.text=@"物流商";
                    expressCompanyName.textColor=shenhuiColor;
                    expressCompanyName.font=[UIFont systemFontOfSize:14];
                    [expressCompanyView addSubview:expressCompanyName];
                    UILabel*expressNo=[[UILabel alloc]initWithFrame:CGRectMake(15, expressCompanyName.bottom+7, 130, 14)];
                    expressNo.text=@"物流单号";
                    expressNo.textColor=shenhuiColor;
                    expressNo.font=[UIFont systemFontOfSize:14];
                    [expressCompanyView addSubview:expressNo];
                    
                    UILabel*expressCompany1=[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-200, expressCompany.bottom+19, 185, contenfot)];
                    expressCompany1.text=[[severDataDict.dataDict objectForKey:@"delivery"] objectForKey:@"expressCompany"];
                    expressCompany1.textAlignment=NSTextAlignmentRight;
                    expressCompany1.textColor=qianheiColor;
                    expressCompany1.font=[UIFont systemFontOfSize:contenfot];
                    [expressCompanyView addSubview:expressCompany1];
                    UILabel*expressCompany2=[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-200, expressCompany1.bottom+7, 185, contenfot)];
                    expressCompany2.text=[[severDataDict.dataDict objectForKey:@"delivery"] objectForKey:@"expressNumber"];
                    expressCompany2.textAlignment=NSTextAlignmentRight;
                    expressCompany2.textColor=qianheiColor;
                    expressCompany2.font=[UIFont systemFontOfSize:contenfot];
                    [expressCompanyView addSubview:expressCompany2];
                    UILabel*courier=[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-200, expressCompany2.bottom+7, 185, contenfot)];
                    courier.text=[[severDataDict.dataDict objectForKey:@"delivery"] objectForKey:@"courier"];
                    courier.textAlignment=NSTextAlignmentRight;
                    courier.textColor=qianheiColor;
                    courier.font=[UIFont systemFontOfSize:contenfot];
                    [expressCompanyView addSubview:courier];
                    
                    UILabel *line=[[UILabel alloc] initWithFrame:CGRectMake(0, expressCompanyView.bottom, ScreenWidth, 0.40)];
                    line.backgroundColor=qianhuiColor;
                    [MYmainScroll addSubview:line];
                  
                    
                    remarkLabelView=[[UIView alloc]initWithFrame:CGRectMake(0, expressCompanyView.bottom+10, ScreenWidth, 90)];
                    remarkLabelView.backgroundColor=[UIColor whiteColor];
                    remarkLabelView.layer.borderWidth = 0.40;
                    remarkLabelView.layer.borderColor = qianhuiColor.CGColor;
                    [MYmainScroll addSubview:remarkLabelView];

                }else{
            
                    remarkLabelView=[[UIView alloc]initWithFrame:CGRectMake(0, CommonView.bottom, ScreenWidth, 90)];
                    remarkLabelView.backgroundColor=[UIColor whiteColor];
                    [MYmainScroll addSubview:remarkLabelView];
                    remarkline=[[UILabel alloc] initWithFrame:CGRectMake(0, 90, ScreenWidth, 0.40)];
                    remarkline.backgroundColor=qianhuiColor;
                    [remarkLabelView addSubview:remarkline];
                }
               
               UILabel*remark=[[UILabel alloc]initWithFrame:CGRectMake(15,15, ScreenWidth*70/750, 15)];
                remark.text=@"备注";
                remark.font= [UIFont systemFontOfSize:15];
                [remark setTextColor:qianheiColor];
                [remarkLabelView addSubview:remark];
               
                    NSString *s1 =[NSString stringWithFormat:@"%@", [[severDataDict.dataDict objectForKey:@"delivery"] objectForKey:@"remark"]];
                    UIFont *font1 = [UIFont systemFontOfSize:contenfot];
                    CGSize size1 = CGSizeMake(ScreenWidth-60, 2000);
                    CGSize labelsize1 = [s1 sizeWithFont:font1 constrainedToSize:size1 lineBreakMode:NSLineBreakByCharWrapping];
                    
                    UILabel*remarkLabel=[[UILabel alloc]initWithFrame:CGRectMake(15, remark.bottom+12, ScreenWidth-60, labelsize1.height+10)];
                
                if ([[[severDataDict.dataDict objectForKey:@"delivery"] objectForKey:@"remark"] length]>1) {
                remarkLabel.text=s1;
                }else{
                   remarkLabel.text=@"无";
                
                }
                
                    remarkLabel.numberOfLines=0;
                    remarkLabel.font= [UIFont systemFontOfSize:contenfot];
                    [remarkLabel setTextColor:shenhuiColor];
                    [remarkLabelView addSubview:remarkLabel];
                   
               
                
                if ([[NSString stringWithFormat:@"%@",  [[dataDict objectForKey:@"delivery"] objectForKey:@"deliveryStatus"]] isEqualToString:@"Y"]) {
                  remarkLabelView=[[UIView alloc]initWithFrame:CGRectMake(0, expressCompanyView.bottom+10, ScreenWidth, remarkLabel.bottom+30)];
                }else{
                remarkLabelView.frame=CGRectMake(0, CommonView.bottom+0.35, ScreenWidth, remarkLabel.bottom+30);
                remarkline.frame=CGRectMake(0, remarkLabel.bottom+30, ScreenWidth, 0.40);

                }
                
                
                [MYmainScroll setContentSize:CGSizeMake(ScreenWidth, remarkLabelView.bottom+100)];
                }else{
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [Global showTextHudSub:requestResultStr view:self.view];
            }
            
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            [Global showTextHudSub:severDataDict.resultStr view:self.view];
        }
        
    }];
}
-(void)memberBtn:(NSString *)str{
    MembershipDetailsViewController *detailViewController = [[MembershipDetailsViewController alloc] init];
    detailViewController.memberId=[NSString stringWithFormat:@"%@",[[dataDict objectForKey:@"member"]objectForKey:@"memberId"]];
    [self.navigationController pushViewController:detailViewController animated:YES];
    
}
- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}

- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}


@end
